package servlets;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import studentSession.StudentSession;
import descriptor.AcademicSupListDescriptor;
import descriptor.CVDescriptor;
import descriptor.CVListDescriptor;
import descriptor.CitiesListDescriptor;
import descriptor.CompaniesListDescriptor;
import descriptor.CompanyDescriptor;
import descriptor.CountriesListDescriptor;
import descriptor.OfferListDescriptor;
import descriptor.QueryListDescriptor;
import descriptor.SectorsListDescriptor;
import descriptor.TimetableDescriptor;
import descriptor.TimetableListDescriptor;
import extras.Md5Encryption;

public class StudentServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private StudentSession studentSession = null;
	RequestDispatcher dispatcher = null ;
	private String action = "";
	
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
	{
		System.out.println("get");
		HttpSession session = req.getSession(true);
		studentSession = (StudentSession)session.getAttribute("studentSession");
		if(studentSession != null)
		{
			if(studentSession.isLogged())
			{
				action = (String) req.getAttribute("action");
				TimetableListDescriptor tbList2 = studentSession.getAllEvents();
				if(tbList2.sizeTimetableList() > 0)
				{
					req.setAttribute("tbList2",tbList2);
				}
				int nbOffers = studentSession.viewAllOffers().sizeOfferList();
				req.setAttribute("nbOffers",nbOffers);
				int nbPersoOffers = studentSession.viewPersonalOffers().sizeOfferList();
				req.setAttribute("nbPersoOffers",nbPersoOffers);
				System.out.println("action = " +(String) req.getAttribute("action"));
				if (action == null)
				{
					req.getSession().setAttribute("login",studentSession.getLogin());
					req.getSession().setAttribute("userSurname",studentSession.getName());
					req.getSession().setAttribute("userFirstname",studentSession.getFirstname());
					req.getSession().setAttribute("mail",studentSession.getMail());
					req.getSession().setAttribute("degree",studentSession.getDegree());
					dispatcher = req.getRequestDispatcher("Student/StudentInformation.jsp");
				}
			}
		}
		else
		{
			req.setAttribute("error1", Notification.getNotification(Notification.appl_identification,""));
			dispatcher = req.getRequestDispatcher("index.jsp");
		}
		if (dispatcher != null) 
			dispatcher.forward(req, res) ;
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
	{
		System.out.println("post");
		HttpSession session = req.getSession(true);
		session.setMaxInactiveInterval(120);
		studentSession = (StudentSession)session.getAttribute("studentSession");
		if(studentSession != null)
		{
			if(studentSession.isLogged())
			{
				action = req.getParameter("action");
				TimetableListDescriptor tbList2 = studentSession.getAllEvents();
				if(tbList2.sizeTimetableList() > 0)
				{
					req.setAttribute("tbList2",tbList2);
				}
				int nbOffers = studentSession.viewAllOffers().sizeOfferList();
				req.setAttribute("nbOffers",nbOffers);
				int nbPersoOffers = studentSession.viewPersonalOffers().sizeOfferList();
				req.setAttribute("nbPersoOffers",nbPersoOffers);
				if (action == null)
				{
					if (ServletFileUpload.isMultipartContent(req))
					{
						DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
						/* if the size of a file item is smaller than the size threshold (40960 bytes), it will be stored in the memory. 
						//Otherwise it will be stored in a temporary file on disk.*/
						//diskFileItemFactory.setSizeThreshold(40960); 
	
						File repositoryPath = new File("/tmp/WorkplacementManagementSystem/StudentCVs/"+studentSession.getLogin());
						if(!repositoryPath.exists())
						{
							if(repositoryPath.mkdirs())
							{
								Notification.getNotification(Notification.dirCVBuilt,studentSession.getLogin());
								System.out.println("Student CV directory built");
							}
						}
						diskFileItemFactory.setRepository(repositoryPath);
	
						ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
						//servletFileUpload.setSizeMax(81920); /* the unit is bytes */
						List items = null;
						try 
						{
							items = servletFileUpload.parseRequest(req);
						} 
						catch (FileUploadException e) {e.printStackTrace();}
						
						Iterator it = items.iterator();
						boolean auth = false;
						while (it.hasNext())
						{
							FileItem item = (FileItem)it.next();
							if (item.isFormField())
							{
								if(item.getFieldName().equals("action"))
								{
									action = item.getString();
									System.out.println(item.getString());
								}
								if(item.getFieldName().equals("auth"))
								{
									auth = Boolean.parseBoolean(item.getString());
									System.out.println(item.getString());
								}
							}
							else
							{
								File fullFile  = new File(item.getName());  
								String contentType = item.getContentType();
								File savedFile = new File(repositoryPath,fullFile.getName());
								try 
								{
									if(contentType.equals("application/pdf"))
									{
										int i = 1;
										while(savedFile.exists())
										{
											fullFile = new File("Copy_"+i+ "_" + fullFile.getName());
											savedFile = new File(repositoryPath,fullFile.getName());
											i++;
										}
										item.write(savedFile);
										if(action.equals("uploadedCV"))
										{
											if(studentSession.uploadCV(fullFile.getName(), repositoryPath+"/"+fullFile.getName(), auth))
											{
												String msg = Notification.getNotification(Notification.cv_ok,studentSession.getLogin() + " - " +savedFile.getAbsolutePath());
												req.setAttribute("msg", msg);
												CVListDescriptor cvsList = studentSession.viewCVs();req.getSession().setAttribute("CV",cvsList);
												dispatcher = req.getRequestDispatcher("Student/StudentCV.jsp");
											}
											else
											{
												req.setAttribute("error1", Notification.getNotification(Notification.cv_upload_error,studentSession.getLogin()));
												dispatcher = req.getRequestDispatcher("Student/StudentCVUpload.jsp");
											}
										}
									}
									else 
									{
										System.out.println("contentType : "+contentType +" filename : "+fullFile.getName());
										req.setAttribute("error1",Notification.getNotification(Notification.cv_not_pdf,""));
										dispatcher = req.getRequestDispatcher("Student/StudentCVUpload.jsp");
									}
								} 
								catch (Exception e) 
								{e.printStackTrace();}
							}
						}
					}
					else
					{
						dispatcher = req.getRequestDispatcher("./UserServletError");
					}
				}
				else
				{
					System.out.println("action =" +action);
					if (action.equals("viewInfo"))
					{
						req.getSession().setAttribute("login",studentSession.getLogin());
						req.getSession().setAttribute("userSurname",studentSession.getName());
						req.getSession().setAttribute("userFirstname",studentSession.getFirstname());
						req.getSession().setAttribute("mail",studentSession.getMail());
						req.getSession().setAttribute("degree",studentSession.getDegree());
						dispatcher = req.getRequestDispatcher("Student/StudentInformation.jsp");
					}
					if (action.equals("logout"))
					{
						req.setAttribute("error1", Notification.getNotification(Notification.logout,studentSession.getLogin()));
						session.invalidate();
						dispatcher = req.getRequestDispatcher("index.jsp");
					}
					if (action.equals("settings"))
					{
						dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
					}
					if (action.equals("viewCVs"))
					{
						CVListDescriptor cvList = studentSession.viewCVs();
						req.getSession().setAttribute("CV",cvList);
						if(cvList.sizeCVList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Student/StudentCV.jsp");
						}
						else
						{
							System.out.println(Notification.getNotification(Notification.no_cv,""));
							req.setAttribute("error2", Notification.getNotification(Notification.no_cv,""));
							dispatcher = req.getRequestDispatcher("Student/StudentCV.jsp");
						}
						
					}
					if (action.equals("viewCV"))
					{
						CVListDescriptor cvList = studentSession.viewCVs();
						req.getSession().setAttribute("CV",cvList);
						if(cvList.sizeCVList() > 0)
						{
							int id = Integer.parseInt(req.getParameter("cvID"));
							CVDescriptor des = studentSession.viewCV(id);
							if(des != null)
							{
								String fileLocation = des.getFileLocation();
								String fileName = des.getFileName();
								ServletOutputStream stream = null;
							    BufferedInputStream buf = null;
							    try 
							    {
							    	stream = res.getOutputStream();
							    	File pdf = new File(fileLocation);
							    	res.setContentType("application/pdf");
							    	res.addHeader("Content-Disposition", "inline; filename=\""+ fileName+"\"");
							    	res.setContentLength((int) pdf.length());
							    	FileInputStream input = new FileInputStream(pdf);
							    	buf = new BufferedInputStream(input);
							    	int readBytes = 0;
							    	while ((readBytes = buf.read()) != -1)
							    		stream.write(readBytes);
							    } 
							    catch (IOException ioe) {throw new ServletException(ioe.getMessage());} 
							    finally 
							    {
							      if (stream != null)
							        stream.close();
							      if (buf != null)
							        buf.close();
							    }
							}
							dispatcher = req.getRequestDispatcher("Student/StudentCV.jsp");
						}
						else
						{
							System.out.println(Notification.getNotification(Notification.no_cv,""));
							req.setAttribute("error2", Notification.getNotification(Notification.no_cv,""));
							dispatcher = req.getRequestDispatcher("Student/StudentCV.jsp");
						}
						
					}
					if (action.equals("changeAuthCV"))
					{
						int cvID = Integer.parseInt(req.getParameter("cvID"));
						boolean cvauth = Boolean.parseBoolean(req.getParameter("cvAuth"));
						if(cvauth)
							cvauth = false;
						else
							cvauth = true;
						if(studentSession.updateCV(cvID,cvauth))
						{
							CVListDescriptor cvsList = studentSession.viewCVs();req.getSession().setAttribute("CV",cvsList);
							dispatcher = req.getRequestDispatcher("Student/StudentCV.jsp");
						}
						else
						{
							System.out.println("error : UpdateCV");
							dispatcher = req.getRequestDispatcher("./UserServletError");
						}
					}
					if (action.equals("deleteCV"))
					{
						String filename = req.getParameter("filename");
						int cvID = Integer.parseInt(req.getParameter("cvID"));
						File fileToDelete = new File("/tmp/WorkplacementManagementSystem/StudentCVs/"+studentSession.getLogin()+"/"+filename);
						if(!fileToDelete.exists())
						{
							Notification.getNotification(Notification.file_unexist, "StudentServlet - " + fileToDelete.getAbsolutePath());
						}
						if(studentSession.deleteCV(cvID))
						{
							if(fileToDelete.delete())
							{
								Notification.getNotification(Notification.cv_delete, studentSession.getLogin());
							}
							else
							{
								Notification.getNotification(Notification.file_delete_error, "StudentServlet - " + fileToDelete.getAbsolutePath());
							}
							CVListDescriptor cvsList = studentSession.viewCVs();
							req.getSession().setAttribute("CV",cvsList);
							dispatcher = req.getRequestDispatcher("Student/StudentCV.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.cv_delete_error, studentSession.getLogin() + fileToDelete.getAbsolutePath()) + fileToDelete.getName());
							dispatcher = req.getRequestDispatcher("Student/StudentCV.jsp");
						}
					}
					if (action.equals("uploadCV"))
					{
						dispatcher = req.getRequestDispatcher("Student/StudentCVUpload.jsp");
					}
					if (action.equals("changeMail"))
					{
						String mail = req.getParameter("email");
						if(!mail.equals("") && mail.matches("(.+?)@(.+?)\\.(.+?)"))
						{
							studentSession.updateMail(mail);
							String msg = Notification.getNotification(Notification.mail_student_ok, studentSession.getLogin());
							req.setAttribute("msg", msg);
							req.getSession().setAttribute("mail",studentSession.getMail());
							dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
						}
						else
						{
							req.setAttribute("error1",Notification.getNotification(Notification.mail_student_exp, ""));
							req.setAttribute("error2",mail);
							dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
						}
						
					}
					if (action.equals("changePasswd"))
					{
						String pass1 = req.getParameter("Oldpasswd");
						String pass2 = req.getParameter("Newpasswd1");
						String pass3 = req.getParameter("Newpasswd2");
						try 
						{
							if(Md5Encryption.testPassword(pass1, studentSession.getPassword()))
							{
								if(pass2.equals(pass3))
								{	
									if(pass2.length()>=5)
									{
										studentSession.updatePasswd(Md5Encryption.getEncodedPassword(pass2));
										String msg2 = Notification.getNotification(Notification.password_student_ok, studentSession.getLogin());
										req.setAttribute("msg2", msg2);
										dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
									}
									else
									{
										req.setAttribute("error3",Notification.getNotification(Notification.password_student_length, ""));
										dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
									}	
									
								}
								else
								{
									req.setAttribute("error3",Notification.getNotification(Notification.password_2_error, ""));
									dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
								}
							}
							else
							{
								req.setAttribute("error3",Notification.getNotification(Notification.password_wrong, studentSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
							}
						} 
						catch (NoSuchAlgorithmException e) 
						{
							req.setAttribute("error3",Notification.getNotification(Notification.password_wrong, "StudentServlet"));
							dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
						}
						
					}
					if (action.equals("academicSup"))
					{
						if(studentSession.getAcademicSup() != null)
						{
							req.getSession().setAttribute("acadmicSupSurame",studentSession.getAcademicSup().getName());
							req.getSession().setAttribute("acadmicSupFirstname",studentSession.getAcademicSup().getFirstName());
							req.getSession().setAttribute("acadmicSupEmail",studentSession.getAcademicSup().getMail());
							req.getSession().setAttribute("acadmicSupDep",studentSession.getAcademicSup().getDepartment());
							dispatcher = req.getRequestDispatcher("Student/StudentAcademicSup.jsp");
						}
						else
						{
							req.setAttribute("error1",Notification.getNotification(Notification.no_academic_sup, ""));
							dispatcher = req.getRequestDispatcher("Student/StudentAcademicSup.jsp");
						}
						
					}
					if (action.equals("companySup"))
					{
						if(studentSession.getCompanySup() != null)
						{
							req.getSession().setAttribute("companySupSurname",studentSession.getCompanySup().getName());
							req.getSession().setAttribute("companySupFirstame",studentSession.getCompanySup().getFirstName());
							req.getSession().setAttribute("companySupEmail",studentSession.getCompanySup().getMail());
							req.getSession().setAttribute("companySupCompany",studentSession.getCompanySup().getCompanyName());
							req.getSession().setAttribute("companySupCompanyID",studentSession.getCompanySup().getCompanyId());
							dispatcher = req.getRequestDispatcher("Student/StudentCompanySup.jsp");
						}
						else
						{
							req.setAttribute("error1",Notification.getNotification(Notification.no_company_sup, ""));
							dispatcher = req.getRequestDispatcher("Student/StudentCompanySup.jsp");
						}
						
					}
					if (action.equals("allCompanies"))
					{
						CompaniesListDescriptor compList = studentSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
						}
					}
					if (action.equals("viewComp1"))
					{
						String id = req.getParameter("companyID");
						CompaniesListDescriptor compList = studentSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(!id.equals(""))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								int compID = Integer.parseInt(id);
								CompanyDescriptor compDes = studentSession.getCompany(compID);
								if(compDes != null)
								{
									req.setAttribute("companyID",id);
									req.setAttribute("CompanyDes",compDes);
									req.setAttribute("viewComp", "1");
									dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.company_notFound,studentSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
							}
						}
					}
					if (action.equals("setCompanies"))
					{
						SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
						req.setAttribute("sectPref",sectPref);
						CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
						req.setAttribute("citiesPref",citiesPref);
						CountriesListDescriptor countriesPref = studentSession.viewCountriesPref();
						req.setAttribute("countriesPref",countriesPref);
						CompaniesListDescriptor compList = studentSession.getAllCompanies();
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
						}
					}
					if (action.equals("setPref"))
					{
						String pref = req.getParameter("pref");
						if(!pref.equals(""))
						{
							CompaniesListDescriptor compList = studentSession.getAllCompanies();
							if(compList.sizeCompaniesList() > 0)
							{
								
								if(pref.equals("1"))
								{
									SectorsListDescriptor sectors = studentSession.getSectors();
									req.setAttribute("sectors",sectors);
									SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
									req.setAttribute("sectPref",sectPref);
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
								}
								if(pref.equals("2"))
								{
									CitiesListDescriptor cities = studentSession.getCities();
									req.setAttribute("cities",cities);
									CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
									req.setAttribute("citiesPref",citiesPref);
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
								}
								if(pref.equals("3"))
								{
									CountriesListDescriptor countries = studentSession.getCountries();
									req.setAttribute("countries",countries);
									CountriesListDescriptor countriesPref = studentSession.viewCountriesPref();
									req.setAttribute("countriesPref",countriesPref);
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
							}
						}
					}
					if (action.equals("prefSet"))
					{
						SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
						req.setAttribute("sectPref",sectPref);
						CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
						req.setAttribute("citiesPref",citiesPref);
						CountriesListDescriptor countriesPref = studentSession.viewCountriesPref();
						req.setAttribute("countriesPref",countriesPref);
						CompaniesListDescriptor compList = studentSession.getAllCompanies();
						if(compList.sizeCompaniesList() > 0)
						{
							String pref = req.getParameter("pref");
							String[] valPref = req.getParameterValues("chbx");
							int size = 0;
							if(valPref != null)
							{
								size = valPref.length;
								Vector<Integer> vecID = new Vector<Integer>();
								for(int i=0 ; i<size ; i++)
								{
									vecID.add(Integer.parseInt(valPref[i]));
								}
								if(pref.equals("1"))
								{
									boolean ok = true;
									if(!studentSession.setSectorPref(vecID))
									{
											ok = false;
									}
									if(ok)
									{
										SectorsListDescriptor sectPref1 = studentSession.viewSectorsPref();
										req.setAttribute("sectPref",sectPref1);
										dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
									}
									else
									{
										req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_save_error1,studentSession.getLogin()));
										dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
									}
								
								}
								if(pref.equals("2"))
								{
									boolean ok = true;
									if(!studentSession.setCityPref(vecID))
									{
										ok = false;
									}
									if(ok)
									{
										CitiesListDescriptor citiesPref1 = studentSession.viewCitiesPref();
										req.setAttribute("citiesPref",citiesPref1);
										dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
									}
									else
									{
										req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_save_error2,studentSession.getLogin()));
										dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
									}
								}
								if(pref.equals("3"))
								{
									boolean ok = true;
									if(!studentSession.setCountryPref(vecID))
									{
										ok = false;
									}
									if(ok)
									{
										CountriesListDescriptor countriesPref1 = studentSession.viewCountriesPref();
										req.setAttribute("countriesPref",countriesPref1);
										dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
									}
									else
									{
										req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_save_error2,studentSession.getLogin()));
										dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
									}
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_no_choose,""));
								dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
						}
					}
					if (action.equals("delPref"))
					{
						CompaniesListDescriptor compList = studentSession.getAllCompanies();
						if(compList.sizeCompaniesList() > 0)
						{
							String pref = req.getParameter("pref");
							if(pref.equals("1"))
							{
								if(studentSession.deleteSectorPref())
								{
									SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
									req.setAttribute("sectPref",sectPref);
									CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
									req.setAttribute("citiesPref",citiesPref);
									CountriesListDescriptor countriesPref = studentSession.viewCountriesPref();
									req.setAttribute("countriesPref",countriesPref);
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_del_error1,studentSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
								}
							}
							if(pref.equals("2"))
							{
								if(studentSession.deleteCityPref())
								{
									SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
									req.setAttribute("sectPref",sectPref);
									CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
									req.setAttribute("citiesPref",citiesPref);
									CountriesListDescriptor countriestPref = studentSession.viewCountriesPref();
									req.setAttribute("countriesPref",countriestPref);
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_del_error2,studentSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
								}
							}
							if(pref.equals("3"))
							{
								if(studentSession.deleteCountryPref())
								{
									SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
									req.setAttribute("sectPref",sectPref);
									CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
									req.setAttribute("citiesPref",citiesPref);
									CountriesListDescriptor countriesPref = studentSession.viewCountriesPref();
									req.setAttribute("countriesPref",countriesPref);
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_del_error3,studentSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferencesSettings.jsp");
								}
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
						}
					}
					if (action.equals("relevantCompanies"))
					{
						SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
						req.setAttribute("sectPref",sectPref);
						CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
						req.setAttribute("citiesPref",citiesPref);
						CountriesListDescriptor countriesPref = studentSession.viewCountriesPref();
						req.setAttribute("countriesPref",countriesPref);
						CompaniesListDescriptor compList = studentSession.getCompanies();
						req.setAttribute("compList",compList);
						CompaniesListDescriptor compPref = studentSession.viewCompaniesPref();
						req.setAttribute("compPref",compPref);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Student/StudentCompaniesMatch.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_nomatch,""));
							dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
						}
					}
					if (action.equals("selectComp"))
					{
						int compID = Integer.parseInt(req.getParameter("companyID"));
						SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
						req.setAttribute("sectPref",sectPref);
						CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
						req.setAttribute("citiesPref",citiesPref);
						CountriesListDescriptor countriesPref = studentSession.viewCountriesPref();
						req.setAttribute("countriesPref",countriesPref);
						CompaniesListDescriptor compList = studentSession.getCompanies();
						req.setAttribute("compList",compList);
						CompaniesListDescriptor compPref = studentSession.viewCompaniesPref();
						req.setAttribute("compPref",compPref);
						if(studentSession.setCompanyPref(compID))
						{
							compPref = studentSession.viewCompaniesPref();
							req.setAttribute("compPref",compPref);
							if(compList.sizeCompaniesList() > 0)
							{
								dispatcher = req.getRequestDispatcher("Student/StudentCompaniesMatch.jsp");
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_nomatch,""));
								dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
							}
						}
						/*else
						{
							req.setAttribute("error2", Notification.getNotification(Notification.comp_pref_saveComp_error,studentSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Student/StudentCompaniesMatch.jsp");
						}*/
					}
					if (action.equals("favouriteComp"))
					{
						CompaniesListDescriptor compList = studentSession.getAllCompanies();
						if(compList.sizeCompaniesList() > 0)
						{
							CompaniesListDescriptor compPref = studentSession.viewCompaniesPref();
							req.setAttribute("compPref",compPref);
							SectorsListDescriptor sectPref = studentSession.viewSectorsPref();
							req.setAttribute("sectPref",sectPref);
							CitiesListDescriptor citiesPref = studentSession.viewCitiesPref();
							req.setAttribute("citiesPref",citiesPref);
							CountriesListDescriptor countriesPref = studentSession.viewCountriesPref();
							req.setAttribute("countriesPref",countriesPref);
							if(compPref.sizeCompaniesList() > 0)
							{
								dispatcher = req.getRequestDispatcher("Student/StudentCompaniesFavourite.jsp");
							}
							else
							{
								if(!(sectPref.sizeSectorList()>0) && !(citiesPref.sizeCitiesList()>0) && !(countriesPref.sizeCountriesList()>0))
								{
									req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_noSet,""));
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_choose,""));
									dispatcher = req.getRequestDispatcher("Student/StudentCompaniesPreferences.jsp");
								}
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Student/StudentCompanies.jsp");
						}
					}
					if (action.equals("deleteCompFav"))
					{
						int compID = Integer.parseInt(req.getParameter("companyID"));
						CompaniesListDescriptor compPref = studentSession.viewCompaniesPref();
						if(studentSession.deleteCompanyPref(compID))
						{
							CompaniesListDescriptor compList = studentSession.getCompanies();
							req.setAttribute("compList",compList);
							compPref = studentSession.viewCompaniesPref();
							req.setAttribute("compPref",compPref);
							if(compPref.sizeCompaniesList()>0)
							{
								dispatcher = req.getRequestDispatcher("Student/StudentCompaniesFavourite.jsp");
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_favourite_delete_all,""));
								dispatcher = req.getRequestDispatcher("Student/StudentCompaniesFavourite.jsp");
							}
								
						}
						else
						{
							req.setAttribute("error2", Notification.getNotification(Notification.comp_pref_delCompPref_error,studentSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Student/StudentCompaniesFavourite.jsp");
						}
					}
					if (action.equals("allQueries"))
					{
						QueryListDescriptor queriesList = studentSession.viewAllQuery();
						req.setAttribute("queriesList",queriesList);
						int sizeList = queriesList.sizeQueriesList();
						if(sizeList>0)
						{
							dispatcher = req.getRequestDispatcher("Student/StudentQueriesView.jsp");
						}
						else
						{
							req.setAttribute("error2", Notification.getNotification(Notification.query_sentList_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentQueriesView.jsp");
						}
						
					}
					if (action.equals("deleteQuery"))
					{
						int id = Integer.parseInt(req.getParameter("queryID"));
						QueryListDescriptor queriesList = studentSession.viewAllQuery();
						req.setAttribute("queriesList",queriesList);
						int sizeList = queriesList.sizeQueriesList();
						if(studentSession.deleteQuery(id))
						{
							if(sizeList>0)
							{
								queriesList = studentSession.viewAllQuery();
								req.setAttribute("queriesList",queriesList);
								dispatcher = req.getRequestDispatcher("Student/StudentQueriesView.jsp");
							}
							else
							{
								req.setAttribute("error2", Notification.getNotification(Notification.query_sentList_empty,""));
								dispatcher = req.getRequestDispatcher("Student/StudentQueriesView.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.query_delete_error,studentSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Student/StudentQueriesView.jsp");
						}
					}
					if (action.equals("viewAcademicSup"))
					{
						String depName = req.getParameter("AcademicSupDep");
						req.setAttribute("dep",depName);
						AcademicSupListDescriptor academicSupList = studentSession.getAllAcademicSup();
						req.setAttribute("AcademicSupList",academicSupList);
						OfferListDescriptor personalOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("personalOfferList",personalOfferList);
						if(!depName.equals("") && depName != null)
						{
							AcademicSupListDescriptor academicSupList2 = studentSession.getAcademicSup(depName);
							req.setAttribute("AcademicSupList2",academicSupList2);
							dispatcher = req.getRequestDispatcher("Student/StudentQueriesSend.jsp");
						}
					}
					if (action.equals("sendQuery"))
					{
						OfferListDescriptor personalOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("personalOfferList",personalOfferList);
						AcademicSupListDescriptor academicSupList = studentSession.getAllAcademicSup();
						req.setAttribute("AcademicSupList",academicSupList);
						if(academicSupList.sizeAcademicSupList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Student/StudentQueriesSend.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.query_no_academicSup,""));
							dispatcher = req.getRequestDispatcher("Student/StudentQueriesSend.jsp");
						}
					}
					if (action.equals("sentQuery"))
					{
						int academicSupID = 0;
						if(!req.getParameter("academicSupID").equals(""))
						{
							academicSupID = Integer.parseInt(req.getParameter("academicSupID"));
						}
						int offerID = 0;
						if(!req.getParameter("offerID").equals("-1"))
						{
							offerID = Integer.parseInt(req.getParameter("offerID"));
						}
						String msg = req.getParameter("msgQuery");
						OfferListDescriptor personalOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("personalOfferList",personalOfferList);
						AcademicSupListDescriptor academicSupList = studentSession.getAllAcademicSup();
						req.setAttribute("AcademicSupList",academicSupList);
						if(studentSession.sendQuery(academicSupID, msg, offerID))
						{
							QueryListDescriptor queriesList = studentSession.viewAllQuery();
							req.setAttribute("queriesList",queriesList);
							int sizeList = queriesList.sizeQueriesList();
							if(sizeList>0)
							{
								dispatcher = req.getRequestDispatcher("Student/StudentQueriesView.jsp");
							}
							else
							{
								req.setAttribute("error2", Notification.getNotification(Notification.query_sentList_empty,""));
								dispatcher = req.getRequestDispatcher("Student/StudentQueriesView.jsp");
							}
						}
						else
						{
							req.setAttribute("error2", Notification.getNotification(Notification.query_not_sent,studentSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Student/StudentQueriesSend.jsp");
						}
					}
					if (action.equals("offerBy"))
					{
						OfferListDescriptor offerList = studentSession.viewAllOffers();
						req.setAttribute("offerList",offerList);
						OfferListDescriptor persoOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("persoOfferList",persoOfferList);
						int sizelist = offerList.sizeOfferList();
						int search = 0;
						if(req.getParameter("searchtype") != null)
							search = Integer.parseInt(req.getParameter("searchtype"));
						if(sizelist > 0)
						{
							if(search != 2)
							{
								dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
							}
							if(search == 2)
							{
								offerList = studentSession.viewAllOffers2();
								req.setAttribute("offerList",offerList);
								if(offerList.sizeOfferList() > 0)
								{
									dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_noSet,""));
									dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
								}
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.offer_list_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
						}
					}
					if (action.equals("persoOffers"))
					{
						OfferListDescriptor persoOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("persoOfferList",persoOfferList);
						if(persoOfferList.sizeOfferList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.offer_persolist_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
						}
					}
					if (action.equals("appliedOffer"))
					{
						OfferListDescriptor persoOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("persoOfferList",persoOfferList);
						
					}
					if (action.equals("applyOffer"))
					{
						int offerID = Integer.parseInt(req.getParameter("offerID"));
						int search = 0;
						if(req.getParameter("searchtype") != null)
							search = Integer.parseInt(req.getParameter("searchtype"));
						OfferListDescriptor offerList = studentSession.viewAllOffers();
						req.setAttribute("offerList",offerList);
						OfferListDescriptor persoOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("persoOfferList",persoOfferList);
						int sizelist = offerList.sizeOfferList();
						if(studentSession.acceptOffer(offerID))
						{
							offerList = studentSession.viewAllOffers();
							persoOfferList = studentSession.viewPersonalOffers();
							req.setAttribute("offerList",offerList);
							req.setAttribute("persoOfferList",persoOfferList);
							/*if(sizelist > 0)
							{
								if(search != 2)
								{
									dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
								}
								if(search == 2)
								{
									offerList = studentSession.viewAllOffers2();
									
									if(offerList.sizeOfferList() > 0)
									{*/
										dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
									/*}
									else
									{
										req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_noSet,""));
										dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
									}
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.offer_list_empty,""));
								dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
							}*/
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.offer_apply_error,studentSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
						}
						
					}
					if (action.equals("acceptOffer"))
					{
						int offerID = Integer.parseInt(req.getParameter("offerID"));
						int search = 0;
						if(req.getParameter("searchtype") != null)
							search = Integer.parseInt(req.getParameter("searchtype"));
						OfferListDescriptor offerList = studentSession.viewAllOffers();
						req.setAttribute("offerList",offerList);
						OfferListDescriptor persoOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("persoOfferList",persoOfferList);
						int sizelist = offerList.sizeOfferList();
						if(studentSession.acceptOffer(offerID))
						{
							offerList = studentSession.viewAllOffers();
							persoOfferList = studentSession.viewPersonalOffers();
							if(sizelist > 0)
							{
								if(search != 2)
								{
									offerList = studentSession.viewAllOffers2();
									req.setAttribute("offerList",offerList);
									dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
								}
								if(search == 2)
								{
									offerList = studentSession.viewAllOffers2();
									req.setAttribute("offerList",offerList);
									if(offerList.sizeOfferList() > 0)
									{
										dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
									}
									else
									{
										req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_noSet,""));
										dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
									}
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.offer_list_empty,""));
								dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.offer_apply_error,studentSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
						}
						
					}
					if (action.equals("acceptOffer2"))
					{
						OfferListDescriptor persoOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("persoOfferList",persoOfferList);
						if(persoOfferList.sizeOfferList() > 0)
						{
							int offerID = Integer.parseInt(req.getParameter("offerID"));
							if(studentSession.acceptOffer(offerID))
							{
								persoOfferList = studentSession.viewPersonalOffers();
								req.setAttribute("persoOfferList",persoOfferList);
								persoOfferList = studentSession.viewPersonalOffers();
								req.setAttribute("persoOfferList",persoOfferList);
								dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.offer_apply_error,studentSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.offer_persolist_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
						}
					}
					if (action.equals("rejectOffer"))
					{
						int offerID = Integer.parseInt(req.getParameter("offerID"));
						int search = 0;
						if(req.getParameter("searchtype") != null)
							search = Integer.parseInt(req.getParameter("searchtype"));
						OfferListDescriptor offerList = studentSession.viewAllOffers();
						req.setAttribute("offerList",offerList);
						OfferListDescriptor persoOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("persoOfferList",persoOfferList);
						int sizelist = offerList.sizeOfferList();
						if(studentSession.rejectOffer(offerID))
						{
							offerList = studentSession.viewAllOffers();
							persoOfferList = studentSession.viewPersonalOffers();
							req.setAttribute("persoOfferList",persoOfferList);
							if(sizelist > 0)
							{
								if(search != 2)
								{
									dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
								}
								if(search == 2)
								{
									offerList = studentSession.viewAllOffers2();
									req.setAttribute("offerList",offerList);
									if(offerList.sizeOfferList() > 0)
									{
										dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
									}
									else
									{
										req.setAttribute("error1", Notification.getNotification(Notification.comp_pref_noSet,""));
										dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
									}
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.offer_list_empty,""));
								dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.offer_apply_error,studentSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Student/StudentOffersGeneral.jsp");
						}
						
					}
					if (action.equals("rejectOffer2"))
					{
						OfferListDescriptor persoOfferList = studentSession.viewPersonalOffers();
						req.setAttribute("persoOfferList",persoOfferList);
						if(persoOfferList.sizeOfferList() > 0)
						{
							int offerID = Integer.parseInt(req.getParameter("offerID"));
							if(studentSession.rejectOffer(offerID))
							{
								persoOfferList = studentSession.viewPersonalOffers();
								req.setAttribute("persoOfferList",persoOfferList);
								dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.offer_apply_error,studentSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.offer_persolist_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentOffersSpecific.jsp");
						}
					}
					if (action.equals("viewDiary"))
					{
						String diary = studentSession.viewDiary1();
						if(!diary.equals("") && diary.length()>0 && diary != null )
						{	
							req.setAttribute("diary",diary);
							dispatcher = req.getRequestDispatcher("Student/StudentDiaryView.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.diary_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentDiaryView.jsp");
						}
						
					}
					if (action.equals("editDiary"))
					{
						String content = null;
						Date date = new Date();
						SimpleDateFormat dateformat = new SimpleDateFormat("EEE, MMM d, ''yy");
						content = dateformat.format(date) + ": ... \n"+ studentSession.viewDiary2();
						req.setAttribute("diary",content);
						dispatcher = req.getRequestDispatcher("Student/StudentDiaryAdd.jsp");
					}
					if (action.equals("editedDiary"))
					{
						studentSession.editDiary(req.getParameter("txtDiary"));
						req.setAttribute("diary",studentSession.viewDiary1());
						dispatcher = req.getRequestDispatcher("Student/StudentDiaryView.jsp");
					}
					if (action.equals("allEvents"))
					{
						TimetableListDescriptor tbList = studentSession.getAllEvents();
						if(tbList.sizeTimetableList() > 0)
						{
							req.setAttribute("tbList",tbList);
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableAllEvents.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.timetable_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableAllEvents.jsp");
						}
					}
					if (action.equals("addEvents"))
					{
						dispatcher = req.getRequestDispatcher("Student/StudentTimetableAddEvent.jsp");
					}
					if (action.equals("editEvent"))
					{
						int id = Integer.parseInt(req.getParameter("eventID"));
						TimetableDescriptor tb = studentSession.editEvent(id);
						if(tb != null)
						{
							req.setAttribute("EventDescriptor",tb);
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableEditEvent.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.timetable_decriptor_error,studentSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableAddEvent.jsp");
						}
					}
					if (action.equals("editedEvent"))
					{
						int id = Integer.parseInt(req.getParameter("eventID"));
						String eventName = req.getParameter("nameEvent");
						String recruiter = req.getParameter("recruiterName");
						String offer = req.getParameter("offerName");
						String date1 = req.getParameter("calendar1");
						if(date1 != null && !date1.equals(""))
						{
							date1 = date1.trim();
							int i1 = date1.indexOf("/");
							int i2 = date1.indexOf("/",i1+1);
							int j = date1.indexOf(" ");
							int k = date1.indexOf(":",j+1);
							int month1 = Integer.parseInt(date1.substring(0,i1));
							int day1 = Integer.parseInt(date1.substring(i1+1, i2));
							int year1 = Integer.parseInt(date1.substring(i2+1,j));
							int hour = Integer.parseInt(date1.substring(j+1,k));
							int minute = Integer.parseInt(date1.substring(k+1));
							if(studentSession.editedEvent(id,eventName, day1, month1, year1, hour, minute, recruiter, offer))
							{
								TimetableListDescriptor tbList = studentSession.getAllEvents();
								req.setAttribute("tbList",tbList);
								dispatcher = req.getRequestDispatcher("Student/StudentTimetableAllEvents.jsp");
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.timetable_event_edit_error,studentSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Student/StudentTimetableAddEvent.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.timetable_date_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableAddEvent.jsp");
						}
					}
					if (action.equals("deleteEvent"))
					{
						int id = Integer.parseInt(req.getParameter("eventID"));
						if(studentSession.deleteEvent(id))
						{
							TimetableListDescriptor tbList = studentSession.getAllEvents();
							if(tbList.sizeTimetableList() > 0)
							{
								req.setAttribute("tbList",tbList);
								dispatcher = req.getRequestDispatcher("Student/StudentTimetableAllEvents.jsp");
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.timetable_empty,studentSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Student/StudentTimetableAllEvents.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.timetable_event_delete_error,""));
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableAllEvents.jsp");
						}
					}
					if (action.equals("dateSelected"))
					{
						System.out.println("day :"+req.getParameter("day"));
						int day = Integer.parseInt(req.getParameter("day"));
						int month = Integer.parseInt(req.getParameter("month"));
						int year = Integer.parseInt(req.getParameter("year"));
						TimetableListDescriptor tbList = studentSession.getEvents(day, month, year);
						if(tbList.sizeTimetableList() > 0)
						{
							req.setAttribute("tbList",tbList);
							req.setAttribute("day",day);
							req.setAttribute("month",month);
							req.setAttribute("year",year);
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableEvents.jsp");
						}
						else
						{
							req.setAttribute("day",day);
							req.setAttribute("month",month);
							req.setAttribute("year",year);
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableAdd1.jsp");
						}
					}
					if (action.equals("addedEvent"))
					{
						String eventName = req.getParameter("nameEvent");
						String recruiter = req.getParameter("recruiterName");
						String offer = req.getParameter("offerName");
						String date1 = req.getParameter("calendar1");
						String date2 = req.getParameter("calendar2");
						if(date1 != null && !date1.equals(""))
						{
							date1 = date1.trim();
							int i1 = date1.indexOf("/");
							int i2 = date1.indexOf("/",i1+1);
							int j = date1.indexOf(" ");
							int k = date1.indexOf(":",j+1);
							int month1 = Integer.parseInt(date1.substring(0,i1));
							int day1 = Integer.parseInt(date1.substring(i1+1, i2));
							int year1 = Integer.parseInt(date1.substring(i2+1,j));
							int hour = Integer.parseInt(date1.substring(j+1,k));
							int minute = Integer.parseInt(date1.substring(k+1));
							if(date2 !=null && !date2.equals(""))
							{
								
								date2 = date2.trim();
								int i4 = date2.indexOf("/");
								int i5 = date2.indexOf("/",i4+1);
								int month2 = Integer.parseInt(date2.substring(0,i4));
								int day2 = Integer.parseInt(date2.substring(i4+1, i5));
								int year2 = Integer.parseInt(date2.substring(i5+1));
								if(month1<=month2 && day1<day2 && year1<=year2)
								{
									if(studentSession.addMultiEvent(eventName, day1, month1, year1, day2, month2, year2))
									{
										TimetableListDescriptor tbList = studentSession.getAllEvents();
										req.setAttribute("tbList",tbList);
										dispatcher = req.getRequestDispatcher("Student/StudentTimetableAllEvents.jsp");
									}
									else
									{
										req.setAttribute("error1", Notification.getNotification(Notification.timetable_multipe_event,studentSession.getLogin()));
										dispatcher = req.getRequestDispatcher("Student/StudentTimetableAddEvent.jsp");
									}
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.timetable_event_date1_sup_error,studentSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Student/StudentTimetableAddEvent.jsp");
								}
							}
							else
							{
								if(studentSession.addEvent(eventName, day1, month1, year1, hour, minute, recruiter, offer))
								{
									TimetableListDescriptor tbList = studentSession.getAllEvents();
									req.setAttribute("tbList",tbList);
									dispatcher = req.getRequestDispatcher("Student/StudentTimetableAllEvents.jsp");
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.timetable_multipe_event,studentSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Student/StudentTimetableAddEvent.jsp");
								}
							}
							
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.timetable_date_empty,""));
							dispatcher = req.getRequestDispatcher("Student/StudentTimetableAddEvent.jsp");
						}
					}
				}
			}
		}
		else
		{
			req.setAttribute("error1", Notification.getNotification(Notification.appl_identification,""));
			dispatcher = req.getRequestDispatcher("index.jsp");
		}
		if (dispatcher != null) 
			dispatcher.forward(req, res) ;
	}

	protected void doPostAndGet(HttpServletRequest req ,HttpServletResponse res)throws IOException, ServletException
	{
	}
}
